<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="mall.demo.mybatis.pojo.ProductInCart" %>
<%@ page import="mall.demo.util.SessionKeyEnum" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购物车</title>
<script type="text/javascript">
function add(ProductId) {
	var num = parseInt(document.getElementById("number"+ProductId).value);
	if (num < 99) {
		document.getElementById("number"+ProductId).value = ++num;
		return;
	}
	if(num >= 99) {
		alert("最多99件");
	}
}
function sub(ProductId) {
	var num = parseInt(document.getElementById("number"+ProductId).value);
	if (num > 1) {
		document.getElementById("number"+ProductId).value = --num;
		return;
	}
	if(num <= 1) {
		alert("最少1件，不需要请删除本条信息，慎重！");
	}
}
function check(ProductId) {
	var num = parseInt(document.getElementById("number"+ProductId).value);
	if(isNaN(num)) {
		alert("请输入正确的数字！");
		document.getElementById("number"+ProductId).value =1;
	}
	if(num > 99) {
		alert("最多99件");
		document.getElementById("number"+ProductId).value =99;
	}
	if(num < 1) {
		alert("最少1件，不需要请删除本条信息，慎重！");
		document.getElementById("number"+ProductId).value =1;
	}
}
function isDel() {
	return confirm("确定删除吗！")
}
</script>
<link href="<%=request.getContextPath() %>/css/bootstrap.css" rel="stylesheet">

<script src="<%=request.getContextPath() %>/js/jquery.js?_=201703042022"></script>
<script src="<%=request.getContextPath() %>/js/bootstrap.js"></script>

</head>
<%
List<ProductInCart> productInCart = (List<ProductInCart>)session.getAttribute(SessionKeyEnum.cart.name());
if(productInCart == null) {
	productInCart = new ArrayList<ProductInCart>();
}
/* Object obj = session.getAttribute(SessionKeyEnum.isUserLoggedIn.name());
boolean isUserLoggedIn = false;
if(obj != null) {
	isUserLoggedIn = (boolean) obj;
} */
%>
<body>
<div class="container">
<jsp:include page="top.jsp"></jsp:include>
<p style="height:50px;">&nbsp;</p>
<table>
	<tr>
	<th>Code</th>
	<th>Name</th>
	<th>Price</th>
	<th>Amount</th>
	<th>操作</th>
	</tr>
	<% 
		int count = 1;
		for (ProductInCart product : productInCart) { %>
	<tr><td><%=product.getCode() %></td>
		<td><%=product.getName() %></td>
		<td><%=product.getPrice() %></td>
		<td><span id="sub" onclick="sub(<%=product.getProductId()%>)">-</span>
			<input type="text" id="number<%=product.getProductId()%>" name="number" value="<%=product.getAmount() %>" size="1" onblur="check(<%=product.getProductId()%>);">
			<span id="add" onclick="add(<%=product.getProductId()%>)">+</span>
		</td>
		<td><a href="<%=request.getContextPath() %>/servlet/delcart.do?id=<%=product.getProductId()%>" onclick="return isDel()">删除本行</a></td>
	</tr>
	<% } %>
</table>
<%-- <% if (isUserLoggedIn && !productInCart.isEmpty()) {%>
	<a href="servlet/order.do">确定下单了</a>
<% } %> --%>

<jsp:include page="footer.jsp"></jsp:include>
</div>
</body>
</html>